/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.xeerkat.xmpp.server.www;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.logging.Level;
import org.infoset.xml.Element;
import org.infoset.xml.InfosetFactory;
import org.infoset.xml.ItemConstructor;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.XMLException;
import org.infoset.xml.util.WriterItemDestination;
import org.restlet.data.CharacterSet;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.OutputRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;
import org.xeerkat.xmpp.ID;
import org.xeerkat.xmpp.server.XMPPServer;
import org.xeerkat.xmpp.server.db.User;

/**
 *
 * @author alex
 */
public class UserResource extends ServerResource {

   public UserResource()
   {
      setNegotiated(false);
   }
   
   public Representation get() {
      XMPPServer server = (XMPPServer)getContext().getAttributes().get("xmpp.server");
      try {
         String id = getRequest().getAttributes().get("id").toString();
         final User user = server.getDatabase().getUser(new ID(id));
         Representation rep = new OutputRepresentation(MediaType.APPLICATION_XML) {
            public void write(OutputStream os)
               throws IOException
            {
               Writer w = new OutputStreamWriter(os,getCharacterSet().toString());
               ItemDestination dest = new WriterItemDestination(w,getCharacterSet().toString());
               ItemConstructor constructor = InfosetFactory.getDefaultInfoset().createItemConstructor();
               try {
                  dest.send(constructor.createDocument());
                  Element userE = constructor.createElement(AdminApplication.USER);
                  userE.setAttributeValue("id", user.getXMPPId().toString());
                  dest.send(userE);
                  try {
                     Iterator<ID> resources = user.getResources();
                     while (resources.hasNext()) {
                        dest.send(constructor.createElement(AdminApplication.RESOURCE));
                        dest.send(constructor.createCharacters(resources.next().toString()));
                        dest.send(constructor.createElementEnd(AdminApplication.RESOURCE));
                     }
                  } catch (SQLException ex) {
                     getLogger().log(Level.SEVERE,"Cannot get resources for user "+user.getXMPPId(),ex);
                  }
                  dest.send(constructor.createElementEnd(AdminApplication.USER));
                  dest.send(constructor.createDocumentEnd());
               } catch (XMLException ex) {
                  throw new IOException(ex.getMessage());
               }
               w.flush();
            }
         };
         rep.setCharacterSet(CharacterSet.UTF_8);
         setStatus(Status.SUCCESS_OK);
         return rep;
      } catch (SQLException ex) {
         getLogger().log(Level.SEVERE,"Cannot get users from database.",ex);
         setStatus(Status.SERVER_ERROR_INTERNAL);
         return new StringRepresentation("Database error.");
      }
   }
}
